<template>
  <div class="app-container">
    <el-form :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      class="form-search"
      label-width="60px">

      <el-form-item label="书名"
        prop="bookName">
        <el-input v-model="queryParams.bookName"
          placeholder="请输入书名"
          clearable
          style="width: 240px"
          maxlength="50"
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery">搜 索</el-button>
        <el-button icon="el-icon-refresh"
          size="mini"
          @click="resetQuery">重 置</el-button>
      </el-form-item>
    </el-form>
    <div class="section-box">
      <el-row :gutter="10"
        class="mb8">
        <el-col :span="1.5">
          <el-button type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd">新增</el-button>
        </el-col>

      </el-row>

      <el-table v-loading="loading"
        :data="dataList">
        <el-table-column label="书名"
          align="center"
          prop="bookName"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column label="封面"
          align="center"
          prop="bookImage"
          :show-overflow-tooltip="true"
          min-width="120">
          <template slot-scope="scope">
            <div>
              <el-image style="width: 50px; height: 50px;"
                :src="scope.row.bookImage"
                :preview-src-list="[scope.row.bookImage]"
                fit="cover"
                preview-teleported="true" />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="作者"
          align="center"
          prop="auth"
          :show-overflow-tooltip="true"
          min-width="160" />
        <el-table-column label="排序"
          align="center"
          prop="sort"
          sortable
          :show-overflow-tooltip="true"
          min-width="100" />
        <el-table-column label="数据状态"
          align="center"
          prop="dataStatus"
          :show-overflow-tooltip="true"
          min-width="120">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.dataStatus"
              active-value="1"
              inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="创建人"
          align="center"
          prop="createBy"
          min-width="140"
          show-overflow-tooltip />
        <el-table-column label="创建时间"
          align="center"
          prop="createTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新人"
          align="center"
          prop="updateBy"
          min-width="140"
          show-overflow-tooltip />
        <el-table-column label="更新时间"
          align="center"
          prop="updateTime"
          min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          min-width="140">
          <template slot-scope="scope">
            <el-button size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button size="mini"
              type="text"
              icon="el-icon-document"
              @click="handleChapter(scope.row)">章节管理</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body>
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">

        <el-form-item label="书名"
          prop="bookName">
          <el-input v-model.trim="form.bookName"
            maxlength="50"
            placeholder="请输入书名">
          </el-input>
        </el-form-item>
        <el-form-item label="作者"
          prop="auth">
          <el-input v-model.trim="form.auth"
            maxlength="50"
            placeholder="请输入作者">
          </el-input>
        </el-form-item>
        <el-form-item label="封面"
          prop="bookImage">
          <ImageUpload :limit="1"
            v-model="form.bookImage" />
        </el-form-item>
        <el-form-item label="排序"
          prop="sort">
          <el-input v-model.trim="form.sort"
            maxlength="10"
            @input="form.sort = onInput(form.sort)"
            placeholder="请输入排序">
          </el-input>
        </el-form-item>
        <el-form-item label="数据状态"
          prop="dataStatus">
          <el-select v-model="form.dataStatus"
            placeholder="请选择数据状态"
            clearable
            style="width: 100%">
            <el-option key="1"
              label="有效"
              value="1" />
            <el-option key="0"
              label="无效"
              value="0" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getBookList,
  addBook,
  updateBook,
  changeBookStatus
} from "@/api/resource/books.js";
import { onInput } from '@/utils/util.js'

export default {
  name: "Books",
  data () {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bookName: null
      },
      // 数据列表
      dataList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created () {
    this.getList();
  },
  methods: {
    onInput,
    /** 查询列表 */
    getList () {
      this.loading = true;
      getBookList(this.queryParams).then(
        (response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 章节管理
    handleChapter (row) {
      const bookId = row.bookId;
      this.$router.push("/resource/books-chapter/chapter/" + bookId);
    },
    // 修改状态
    handleStatusChange (row) {
      // let text = row.dataStatus === "0" ? "启用" : "停用";
      let text = '';
      let tips = ''
      if (row.dataStatus === "1") {
        text = `确认启用 ${row.bookName} ?`
        tips = '启用成功'
      } else {
        text = `确认禁用 ${row.bookName} ?`
        tips = '禁用成功'
      }
      this.$modal
        // .confirm('确认要"' + text + '""' + row.roleName + '"吗？')
        .confirm(text)
        .then(function () {
          return changeBookStatus({
            dataStatus: row.dataStatus,
            bookId: row.bookId
          });
        })
        .then(() => {
          this.$modal.msgSuccess(tips);
        })
        .catch(function () {
          row.dataStatus = row.dataStatus === "0" ? "1" : "0";
        });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        bookName: null,
        bookImage: null,
        sort: null,
        dataStatus: null,
        auth: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = '新增书籍';
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row));
      this.open = true;
      this.title = '修改书籍';
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.bookId != undefined) {
            updateBook(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addBook(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  },
};
</script>
